package day18.Junit_reflect_annotation_proxy.reflectdemo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;

public class fight
{
    public static void main(String[] args) throws Exception {
        //目的:传入任意类型的数据，都可以把里面的字段打印到txt文件里面

       SaveDataTotxt(new Dog2("金毛",3,"Eat"));
        SaveDataTotxt(new Student("小明",18,99.0));
    }
    public static void SaveDataTotxt(Object obj) throws Exception
            /* //调用obj的class方法
//        Class c1=obj.getClass();
//  //      在 方法内部，obj.getClass() 会返回 obj 的实际运行时类型。
////        由于 obj 是 Integer 对象，c1 将是 Integer.class。*/
    {
        OutputStream out = new FileOutputStream("src\\day18\\Junit_reflect_annotation_proxy\\reflectdemo\\测试.txt",true);
        PrintStream ps = new PrintStream(out);
        Class c1=obj.getClass();
        ps.println(c1.getSimpleName());
        Field[] fds=c1.getDeclaredFields();
        for(Field f:fds)
        {
            f.setAccessible(true);
            ps.println(f.getName()+" "+f.get(obj));
        }
        ps.flush();
        ps.close();


    }
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Dog2
{
   private String name;
    private int age;
   private String hobby;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Student
{
    private String name;
    private int age;
    private double score;
}
